/*
 * AbstractSQLNoticiaDAO.java
 *
 * Created on 31 de octubre de 2007, 16:26
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.noticia.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import webportlet.model.noticia.dto.NoticiaDTO;
import webportlet.model.util.InstanceNotFoundException;
import webportlet.model.util.InternalErrorException;
import webportlet.model.util.*;

/**
 *
 * @author Administrador
 */
public abstract class AbstractSQLNoticiaDAO implements SQLNoticiaDAO {

    public abstract NoticiaDTO create(Connection connection, NoticiaDTO noticiaDTO) 
    throws DuplicateInstanceException, InternalErrorException;
    public boolean exits(Connection connection, Long id) 
        throws InternalErrorException
    {
     
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        
        
        try
        {
            String queryString = "SELECT idNoticia FROM Noticia" + " WHERE idNoticia = ?";
            
            preparedStatement = connection.prepareStatement(queryString);
            
            int i=1;
            
            preparedStatement.setLong(i++,id);
            
            resultSet =preparedStatement.executeQuery();
            
           return resultSet.next();
            
         }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
        
    }
    public NoticiaDTO find(Connection connection, Long id)
     throws InstanceNotFoundException, InternalErrorException 
    {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet=null;
        
        try
        {
            String queryString = "SELECT genero,titulo,descripcion" + " FROM Noticia" + 
                    " WHERE idNoticia=?";
            
            preparedStatement = connection.prepareStatement(queryString);
            int i=1;
            preparedStatement.setLong(i++,id);
            
            resultSet = preparedStatement.executeQuery();
            
            if (!resultSet.next()) {
               throw new InstanceNotFoundException(id,NoticiaDTO.class.getName());
            }
            
            i=1;
            
            String genero = resultSet.getString(i++);
            String titulo = resultSet.getString(i++);
            String descripcion = resultSet.getString(i++);
            NoticiaDTO noticiaDTO = new NoticiaDTO(id,genero,titulo,descripcion);
            
            return noticiaDTO;
            
            
        } catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }    
        
        
    }
    public void update(Connection connection, NoticiaDTO noticiaDTO) 
        throws InstanceNotFoundException, InternalErrorException{
        
        PreparedStatement preparedStatement = null;
     
        try
        {
            String queryString = "UPDATE Noticia" + " SET genero=?, titulo = ?, descripcion = ?" +
                    " WHERE idNoticia=?";
            
            preparedStatement = connection.prepareStatement(queryString);
            int i=1;
            
            
            preparedStatement.setString(i++,noticiaDTO.getGenero());
            preparedStatement.setString(i++,noticiaDTO.getTitulo());
            preparedStatement.setString(i++,noticiaDTO.getDescripcion());
            preparedStatement.setLong(i++,noticiaDTO.getId());
            
            int updateRows = preparedStatement.executeUpdate();
            
            if (updateRows == 0) {
                throw new InstanceNotFoundException(noticiaDTO.getId(), "Noticia");
            }

            if (updateRows > 1) {
                throw new SQLException("Nombres duplicados para la fila = '" + 
                    noticiaDTO.getId() + "' in table 'Noticia'");
            }        
            
            
        }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }           
       
        
        
    }
    public void remove(Connection connection, Long id)
         throws InstanceNotFoundException, InternalErrorException
    {
         PreparedStatement preparedStatement = null;
         
         try
         {
            String queryString = "DELETE FROM Noticia WHERE" +
                " idNoticia = ?";
            
            
            preparedStatement = connection.prepareStatement(queryString);
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++,id);
            
            int removedRows = preparedStatement.executeUpdate();
            
            if(removedRows==0)
            {
                throw new InstanceNotFoundException(id, NoticiaDTO.class.getName());
            }
          }catch (SQLException e) {
            throw new InternalErrorException(e);    
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }    
    
        
    
    }
     public ArrayList <NoticiaDTO> recuperarLista(Connection connection, 
            ArrayList <Long> listaIdentificadores)
            throws InstanceNotFoundException, InternalErrorException{
        
        ArrayList <NoticiaDTO> listaNoticias = new ArrayList();
        for(int i=0; i<listaIdentificadores.size(); i++){
            
            if(!listaNoticias.add(this.find(connection,
                    listaIdentificadores.get(i)))){
                throw new InstanceNotFoundException(listaIdentificadores.get(i),
                    NoticiaDTO.class.getName());
            }
        }
        return listaNoticias;
    }
 
 
}
